package com.shixulin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.DTO.ExamPaperQueryDTO;
import com.shixulin.entity.ExamPaper;
import com.shixulin.entity.News;
import com.shixulin.entity.PaperQuestion;
import com.shixulin.mapper.ExamPaperMapper;
import com.shixulin.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
 * (ExamPaper)表服务实现类
 *
 * @author makejava
 * @since 2023-11-20 18:21:07
 */
@Service("examPaperService")
@Transactional
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper> implements ExamPaperService {
    @Autowired
    private ExamPaperMapper examPaperMapper;
    @Override
    @Transactional
    public ResponseResult createExamPaper(ExamPaper examPaper) {
        int insert = this.baseMapper.insert(examPaper);
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paperId",examPaper.getPaperId());
        try {
            if(insert>0){
                //插入试卷创建时间
                LocalDateTime currentDateTime = LocalDateTime.now();;
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String formatTime = currentDateTime.format(formatter);
                ExamPaper createTime = new ExamPaper();
                createTime.setCreateTime(formatTime);
                createTime.setPaperId(examPaper.getPaperId());
                //初始化的试卷为0
                createTime.setExamStatus(0);
                boolean flag = examPaperMapper.UpdateCreateTime(createTime);
                if (flag) {
                    return new ResponseResult(300,"插入成功");
                }else {
                    //时间插入失败的话也删除插入的试卷

                    examPaperMapper.delete(queryWrapper);
                    return new ResponseResult(505,"插入失败");
                }
            }else{
                return new ResponseResult(506,"插入失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            examPaperMapper.delete(queryWrapper);
            return new ResponseResult(406,"错误");
        }
    }

    @Override
    public ResponseResult getExamPaperList(ExamPaperQueryDTO examPaperQueryDTO) {
        try{
            Page<ExamPaper> rowPage = new Page(examPaperQueryDTO.getCurrent(), examPaperQueryDTO.getSize());
            //queryWrapper组装查询where条件
            LambdaQueryWrapper<ExamPaper> queryWrapper = new LambdaQueryWrapper<>();
            rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
            if (rowPage != null) {
                return new ResponseResult(300,"查询成功",rowPage);
            }else {
                return new ResponseResult(505,"查询失败");
            }

        }catch (Exception e) {
            return new ResponseResult(405,"错误");
        }
    }

    @Override
    public ResponseResult publishPaper(int paperId) {

        ExamPaper examPaper = new ExamPaper();
        examPaper.setPaperId(paperId);
        try{
          boolean flag =  examPaperMapper.publishPaper(examPaper);
          if (flag) {
              return new ResponseResult(300,"发布成功");
          }else {
              return new ResponseResult(506,"发布失败");
          }
        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }

    }

    @Override
    public ResponseResult cancelPublish(int paperId) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setPaperId(paperId);
        try{
            boolean flag =  examPaperMapper.cancelPublish(examPaper);
            if (flag) {
                return new ResponseResult(300,"发布成功");
            }else {
                return new ResponseResult(506,"发布失败");
            }
        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }
    }

    @Override
    public ResponseResult getExamPaperByPaperId(int paperId) {
//        ExamPaper examPaper = new ExamPaper();
//        examPaper.setPaperId(paperId);
        try{
            ExamPaper examPaper = examPaperMapper.getExamPaperByPaperId(paperId);
            if (examPaper!=null) {
                return new ResponseResult(300,"查询成功",examPaper);
            }else {
                return new ResponseResult(506,"查询失败");
            }
        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }

    }

    @Override
    public ResponseResult getExamQuestionByPaperId(int paperId) {

        try{
            List<PaperQuestion>paperQuestions = examPaperMapper.getExamQuestionByPaperId(paperId);
            if (paperQuestions != null) {
                return new ResponseResult(300,"查询成功",paperQuestions);
            }else {
                return new ResponseResult(506,"查询失败");
            }

        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }
    }

    @Override
    public ResponseResult editExamPaper(ExamPaper examPaper) {

        try{
            int updateExamPaper = examPaperMapper.updateById(examPaper);
            if (updateExamPaper >0 ) {
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(505,"修改失败");
            }

        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }

    }

    @Override
    public ResponseResult updateExamScore(ExamPaper examPaper) {
        try{
            int updateExamScore = examPaperMapper.updateExamScore(examPaper);
            if (updateExamScore >0 ) {
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(505,"修改失败");
            }

        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }
    }

    @Override
    public ResponseResult deleteExamPaper(int paperId) {
        try{
            int deleteExamPaper = examPaperMapper.deleteExamPaper(paperId);
            if (deleteExamPaper >0 ) {
                return new ResponseResult(300,"删除成功");
            }else {
                return new ResponseResult(505,"删除失败");
            }

        }catch (Exception e) {
            return new ResponseResult(406,"错误");
        }

    }

    @Override
    public ResponseResult getExamPaper(int subjectId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("subject_id",subjectId);

        List<ExamPaper> examPapers = this.baseMapper.selectList(queryWrapper);
        try{
            if (examPapers!=null&&!examPapers.isEmpty()){
                return new ResponseResult<>(300,"查询成功",examPapers);
            }else{
                return new ResponseResult<>(505,"查询失败",examPapers);
            }

        }catch (Exception e){
            System.out.println(e);
            return new ResponseResult(406,"错误");
        }
    }

    @Override
    public ResponseResult getPaper(int paperId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("paper_id",paperId);
        ExamPaper examPaper = this.baseMapper.selectOne(queryWrapper);
        try{
            if (examPaper!=null){
                return new ResponseResult(300,"查询成功",examPaper);
            }else{
                return new ResponseResult(505,"查询失败",examPaper);
            }

        }catch (Exception e){
            System.out.println(e);
            return new ResponseResult(406,"错误");
        }
    }
}
